/*
 * pl_comm.c
 *
 *  Created on: 2023年2月23日
 *      Author: Lw
 */
#include "dri_hal_can.h"
#include "fpga_reg.h"
#include "pl_comm.h"
#include "tele_data_transfer.h"
#include "update_firmware.h"
#include "xil_cache.h"
#include "camera_process.h"
#include "cali_panel.h"
#include "dri_hal_fatfs.h"
#include "ff.h"

extern S_tEquipmentStatus g_tEquipmentStatus;		/* 设备的状态信息结构体 */

void Set_Flat_Dark_process(u8 ucNum, u32 uiValue);
void Set_block_size_0(u8 ucNum, u32 uiValue);
void Set_block_size_1(u8 ucNum, u32 uiValue);
void Set_blcok0_Nmax0(u8 ucNum, u32 uiValue);
void Set_blcok1_Nmax1(u8 ucNum, u32 uiValue);
void Set_blcok_Nmin(u8 ucNum, u32 uiValue);
void Set_corner_cnt(u8 ucNum, u32 uiValue);
void Set_atomosphere_thresh(u8 ucNum, u32 uiValue);
void Set_max_cfs_npix(u8 ucNum, u32 uiValue);
void Set_binarized_thresh(u8 ucNum, u32 uiValue);
void Set_saturated_thresh(u8 ucNum, u32 uiValue);
void Set_object_thresh(u8 ucNum, u32 uiValue);
void Set_min_streak_npix(u8 ucNum, u32 uiValue);
void Set_max_streak_npix(u8 ucNum, u32 uiValue);
void Set_porosity_grade(u8 ucNum, u32 uiValue);
void Set_Target_detection_num(u8 ucNum, u32 uiValue);
void Set_sub_filtering_en(u8 ucNum, u32 uiValue);
void Set_atomosphere_mask_en(u8 ucNum, u32 uiValue);
void Set_fill_defect_mask_en(u8 ucNum, u32 uiValue);
void Set_bright_region_thresh(u8 ucNum, u32 uiValue);
void Set_flat_dark_dn_offset(u8 ucNum, u32 uiValue);
void Set_3sita_value(u8 ucNum, u32 uiValue);
void Set_cali_norm_value(u8 ucNum, u32 uiValue);
void Set_scale_factor(u8 ucNum, u32 uiValue);
void Set_length_width_ratio(u8 ucNum, u32 uiValue);
void Set_corr_coef_thresh0(u8 ucNum, u32 uiValue);
void Set_corr_coef_thresh1(u8 ucNum, u32 uiValue);

u8 DeadPixelMask_Init(u8 Mask_No);
static FIL fil;     /* File object */


/*上电配置参数*/
FRESULT plCommInit(void)
{
    FRESULT rc;
    UINT br;
    UINT bw;
    /* 相机1预处理算法参数存储文件 */
    rc = f_open(&fil, EMMC_CAM1_PRPE_PARAM_FILE, FA_OPEN_EXISTING);
    if(rc != FR_OK){
        rc = f_open(&fil, EMMC_CAM1_PRPE_PARAM_FILE, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
            return rc;
        }
		f_lseek(&fil, 0);
		rc = f_write(&fil, &s_Cam1PrepParam, sizeof(s_Cam1PrepParam), &bw);
    }
    /* 从emmc中读出数据  */
    rc = f_open(&fil, EMMC_CAM1_PRPE_PARAM_FILE, FA_READ);
    rc = f_read(&fil, &s_Cam1PrepParam, sizeof(s_Cam1PrepParam), &br);
    if(rc != FR_OK){
        f_close(&fil);
        return rc;
    }
    f_close(&fil);

    /* 相机2预处理算法参数存储文件 */
    rc = f_open(&fil, EMMC_CAM2_PRPE_PARAM_FILE, FA_OPEN_EXISTING);
    if(rc != FR_OK){
        rc = f_open(&fil, EMMC_CAM2_PRPE_PARAM_FILE, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
            return rc;
        }
		f_lseek(&fil, 0);
		rc = f_write(&fil, &s_Cam2PrepParam, sizeof(s_Cam2PrepParam), &bw);
    }
    /* 从emmc中读出数据  */
    rc = f_open(&fil, EMMC_CAM2_PRPE_PARAM_FILE, FA_READ);
    rc = f_read(&fil, &s_Cam2PrepParam, sizeof(s_Cam2PrepParam), &br);
    if(rc != FR_OK){
        f_close(&fil);
        return rc;
    }
    f_close(&fil);

     Set_Flat_Dark_process(1, s_Cam1PrepParam.Flat_Dark_process_en);
     Set_block_size_0(1, s_Cam1PrepParam.block_size_0);
     Set_block_size_1(1, s_Cam1PrepParam.block_size_1);
     Set_blcok0_Nmax0(1, s_Cam1PrepParam.blcok0_Nmax0);
     Set_blcok1_Nmax1(1, s_Cam1PrepParam.blcok1_Nmax1);
     Set_blcok_Nmin(1, s_Cam1PrepParam.blcok_Nmin);
     Set_corner_cnt(1, s_Cam1PrepParam.corner_cnt);
     Set_atomosphere_thresh(1, s_Cam1PrepParam.atomosphere_thresh);
     Set_max_cfs_npix(1, s_Cam1PrepParam.max_cfs_npix);
     Set_binarized_thresh(1, s_Cam1PrepParam.binarized_thresh);
     Set_saturated_thresh(1, s_Cam1PrepParam.saturated_thresh);
     Set_object_thresh(1, s_Cam1PrepParam.object_thresh);
     Set_min_streak_npix(1, s_Cam1PrepParam.min_streak_npix);
     Set_max_streak_npix(1, s_Cam1PrepParam.max_streak_npix);
     Set_porosity_grade(1, s_Cam1PrepParam.porosity_grade);
     Set_Target_detection_num(1, s_Cam1PrepParam.Target_detection_num);
     Set_sub_filtering_en(1, s_Cam1PrepParam.sub_filtering_en);
     Set_atomosphere_mask_en(1, s_Cam1PrepParam.atomosphere_mask_en);
     Set_fill_defect_mask_en(1, s_Cam1PrepParam.fill_defect_mask_en);
     Set_bright_region_thresh(1, s_Cam1PrepParam.bright_region_thresh);
     Set_flat_dark_dn_offset(1, s_Cam1PrepParam.flat_dark_dn_offset);
     Set_3sita_value(1, s_Cam1PrepParam.sita3_value);
     Set_cali_norm_value(1, s_Cam1PrepParam.cali_norm_value);
     Set_scale_factor(1, s_Cam1PrepParam.scale_factor);
     Set_length_width_ratio(1, s_Cam1PrepParam.length_width_ratio);
     Set_corr_coef_thresh0(1, s_Cam1PrepParam.corr_coef_thresh0);
     Set_corr_coef_thresh1(1, s_Cam1PrepParam.corr_coef_thresh1);

     Set_Flat_Dark_process(2, s_Cam2PrepParam.Flat_Dark_process_en);
     Set_block_size_0(2, s_Cam2PrepParam.block_size_0);
     Set_block_size_1(2, s_Cam2PrepParam.block_size_1);
     Set_blcok0_Nmax0(2, s_Cam2PrepParam.blcok0_Nmax0);
     Set_blcok1_Nmax1(2, s_Cam2PrepParam.blcok1_Nmax1);
     Set_blcok_Nmin(2, s_Cam2PrepParam.blcok_Nmin);
     Set_corner_cnt(2, s_Cam2PrepParam.corner_cnt);
     Set_atomosphere_thresh(2, s_Cam2PrepParam.atomosphere_thresh);
     Set_max_cfs_npix(2, s_Cam2PrepParam.max_cfs_npix);
     Set_binarized_thresh(2, s_Cam2PrepParam.binarized_thresh);
     Set_saturated_thresh(2, s_Cam2PrepParam.saturated_thresh);
     Set_object_thresh(2, s_Cam2PrepParam.object_thresh);
     Set_min_streak_npix(2, s_Cam2PrepParam.min_streak_npix);
     Set_max_streak_npix(2, s_Cam2PrepParam.max_streak_npix);
     Set_porosity_grade(2, s_Cam2PrepParam.porosity_grade);
     Set_Target_detection_num(2, s_Cam2PrepParam.Target_detection_num);
     Set_sub_filtering_en(2, s_Cam2PrepParam.sub_filtering_en);
     Set_atomosphere_mask_en(2, s_Cam2PrepParam.atomosphere_mask_en);
     Set_fill_defect_mask_en(2, s_Cam2PrepParam.fill_defect_mask_en);
     Set_bright_region_thresh(2, s_Cam2PrepParam.bright_region_thresh);
     Set_flat_dark_dn_offset(2, s_Cam2PrepParam.flat_dark_dn_offset);
     Set_3sita_value(2, s_Cam2PrepParam.sita3_value);
     Set_cali_norm_value(2, s_Cam2PrepParam.cali_norm_value);
     Set_scale_factor(2, s_Cam2PrepParam.scale_factor);
     Set_length_width_ratio(2, s_Cam2PrepParam.length_width_ratio);
     Set_corr_coef_thresh0(2, s_Cam2PrepParam.corr_coef_thresh0);
     Set_corr_coef_thresh1(2, s_Cam2PrepParam.corr_coef_thresh1);

    rc = DeadPixelMask_Init(0); /*上电像素坏点Mask初始化配置*/
    rc = DeadPixelMask_Init(1); /*上电像素坏点Mask初始化配置*/
     s_CamPrepFlag.cm1SaveFlag = 0;
     s_CamPrepFlag.cm2SaveFlag = 0;
    return rc;
}

/*************************************************************************
 * 函数名称： DeadPixelMask_Init(u8 Mask_No)
 * 功能：        像素坏点Mask上电初始化
 **************************************************************************
 * 输入变量：     Mask_No  像素Mask序号
 * 输出变量：     无
 * 返回值：      无
 *-----------------------------------------------------------------------
 * 日期                              修改人                  修改内容
 *-----------------------------------------------------------------------
 * 2023.4.11      袁梓翔                  创建
 * xxxx.xx.xx     XXX         添加/修改了XXXXXXXXXXX
 *************************************************************************/
u8 DeadPixelMask_Init(u8 Mask_No)
{
    FRESULT rc;
    UINT br;
    UINT bw;
    
    u8 buff[EMMC_DeadPixelMask_SIZE] = {0};
    
    char *mask_f_name[2] = {EMMC_DeadPixelMask_1_FILE_NAME, EMMC_DeadPixelMask_2_FILE_NAME};
    char *mask_f_init_name[2] = {EMMC_DeadPixelMask_INIT_1_FILE_NAME, EMMC_DeadPixelMask_INIT_2_FILE_NAME};
    u32 mask_baseaddr[2] = {CAM1MASK_BASEADDR, CAM2MASK_BASEADDR};

    memset(buff, 0xff, (EMMC_DeadPixelMask_SIZE - 4));
    rc = f_open(&fil, mask_f_name[Mask_No], FA_OPEN_EXISTING); /* 检测是否存在坏点执行Mask(mask_f_name) */
    if(rc != FR_OK) {
        rc = f_open(&fil, mask_f_init_name[Mask_No], FA_OPEN_EXISTING); /* 检测是否存在坏点出厂Mask(mask_f_init_name) */
        if(rc != FR_OK) {
            rc = f_open(&fil, mask_f_init_name[Mask_No], FA_CREATE_ALWAYS | FA_WRITE); /* 不存在坏点出厂Mask(mask_f_init_name)则创建 */
            if(rc != FR_OK) {
                return rc;
            }
            memset((u32 *)mask_baseaddr[Mask_No], 0xff, EMMC_DeadPixelMask_SIZE); /* 初始化DDR上的坏点Mask */
            f_lseek(&fil, 0);
            rc = f_write(&fil, &buff, EMMC_DeadPixelMask_SIZE, &bw);
            f_close(&fil);
        } else {
            f_lseek(&fil, 0);
            rc = f_read(&fil, &buff, EMMC_DeadPixelMask_SIZE, &br); /* 读取坏点出厂Mask(mask_f_init_name) */
            f_close(&fil);
            if((rc != FR_OK) || (br != EMMC_DeadPixelMask_SIZE)) {
                f_unlink(mask_f_init_name[Mask_No]);
                return FR_DISK_ERR;
            }
        }
        memcpy((void *)mask_baseaddr[Mask_No], buff, EMMC_DeadPixelMask_SIZE); /* 赋值DDR上的坏点Mask */
        rc = f_open(&fil, mask_f_name[Mask_No], FA_CREATE_ALWAYS | FA_WRITE); /* 创建坏点执行Mask(mask_f_name) */
        if(rc != FR_OK) {
            return rc;
        }
        f_lseek(&fil, 0);
        rc = f_write(&fil, &buff, EMMC_DeadPixelMask_SIZE, &bw); /* 保存坏点执行Mask(mask_f_name) */
        f_close(&fil);
    }
    rc = emmcReadData(mask_f_name[Mask_No], buff, EMMC_DeadPixelMask_SIZE);
    if(rc != FR_OK) {
        return rc;
    }
    memcpy((void *)mask_baseaddr[Mask_No], buff, EMMC_DeadPixelMask_SIZE); /* 赋值DDR上的坏点Mask */
    Xil_DCacheFlushRange(mask_baseaddr[Mask_No], EMMC_DeadPixelMask_SIZE);
    memcpy(&g_tEquipmentStatus.DeadPixelNum[Mask_No], &buff[EMMC_DeadPixelMask_SIZE - 4], 4);

    return 0;
}

/*************************************************************************
 * 函数名称： CAM_PixelMask_ResFacSet(int CAM_No)
 * 功能：像素坏点Mask恢复出厂设置
 **************************************************************************
 * 输入变量：     Mask_No  像素Mask序号
 * 输出变量：     无
 * 返回值：      无
 *-----------------------------------------------------------------------
 * 日期                              修改人                  修改内容
 *-----------------------------------------------------------------------
 * 2023.4.11      袁梓翔                  创建
 * xxxx.xx.xx     XXX         添加/修改了XXXXXXXXXXX
 *************************************************************************/

void CAM_PixelMask_ResFacSet(int CAM_No)
{
    FRESULT rc;
    u8 buff[EMMC_DeadPixelMask_SIZE] = {0};
    char *mask_f_name[2] = {EMMC_DeadPixelMask_1_FILE_NAME, EMMC_DeadPixelMask_2_FILE_NAME};
    char *mask_f_init_name[2] = {EMMC_DeadPixelMask_INIT_1_FILE_NAME, EMMC_DeadPixelMask_INIT_2_FILE_NAME};
    u32 mask_baseaddr[2] = {CAM1MASK_BASEADDR, CAM2MASK_BASEADDR};
    
    rc = emmcReadData(mask_f_init_name[CAM_No], buff, EMMC_DeadPixelMask_SIZE);
    if(rc != FR_OK) {
        return;
    }
    memcpy((void *)mask_baseaddr[CAM_No], buff, EMMC_DeadPixelMask_SIZE); /* 赋值DDR上的坏点Mask */
    Xil_DCacheFlushRange(mask_baseaddr[CAM_No], EMMC_DeadPixelMask_SIZE);
    memcpy(&g_tEquipmentStatus.DeadPixelNum[CAM_No], &buff[EMMC_DeadPixelMask_SIZE - 4], 4);
    rc = emmcWriteData(mask_f_name[CAM_No], buff, EMMC_DeadPixelMask_SIZE, 0);
    if(rc != FR_OK) {
        return;
    }
    return;
}

/*************************************************************************
 * 函数名称：operate_bit(unsigned char *bitmap, size_t index, int value)
 * 功能：对指定位置的bit位进行操作
 **************************************************************************
 * 输入变量：     bitmap  位图内存数据
 *          index  位序号
 *          value  置值
 * 输出变量：     无
 * 返回值：      无
 *-----------------------------------------------------------------------
 * 日期                              修改人                  修改内容
 *-----------------------------------------------------------------------
 * 2023.4.11      袁梓翔                  创建
 * xxxx.xx.xx     XXX         添加/修改了XXXXXXXXXXX
 *************************************************************************/

void operate_bit(unsigned char *bitmap, size_t index, int value)
{
    if(bitmap == NULL) {
        return;
    }
    size_t byte_index = index / 8; // 计算字节索引
    unsigned char mask = 1 << (index % 8); // 计算二进制位掩码
    if(value) {
        bitmap[byte_index] |= mask; // 将指定位置的bit位设置为1
    } else {
        bitmap[byte_index] &= ~mask; // 将指定位置的bit位设置为0
    }
}

/*************************************************************************
 * 函数名称：get_bit(unsigned char *bitmap, size_t index)
 * 功能：获取指定位置的bit位的值
 **************************************************************************
 * 输入变量：     bitmap  位图内存数据
 *          index  位序号
 * 输出变量：
 * 返回值：      bit位的值
 *-----------------------------------------------------------------------
 * 日期                              修改人                  修改内容
 *-----------------------------------------------------------------------
 * 2023.4.11      袁梓翔                  创建
 * xxxx.xx.xx     XXX         添加/修改了XXXXXXXXXXX
 *************************************************************************/

int get_bit(unsigned char *bitmap, size_t index)
{
    if(bitmap == NULL) {
        return 0;
    }
    size_t byte_index = index / 8; // 计算字节索引
    unsigned char mask = 1 << (7 - (index % 8)); // 计算二进制位掩码
    return (bitmap[byte_index] & mask) != 0; // 返回指定位置的bit位的值
}

/*************************************************************************
 * 函数名称：countBinaryZeros(void *addr, size_t size)
 * 功能：获取地址块内二进制0的个数
 **************************************************************************
 * 输入变量：     addr  起始地址
 *          size  大小
 * 输出变量：
 * 返回值：      count 二进制0的个数
 *-----------------------------------------------------------------------
 * 日期                              修改人                  修改内容
 *-----------------------------------------------------------------------
 * 2023.5.30      袁梓翔                  创建
 * xxxx.xx.xx     XXX         添加/修改了XXXXXXXXXXX
 *************************************************************************/

int countBinaryZeros(void *addr, size_t size)
{
    unsigned char *ptr = (unsigned char *)addr;
    int count = 0;
    for(size_t i = 0; i < size; i++) {
        unsigned char byte = *ptr++;
        for(int j = 0; j < 8; j++) {
            if((byte & (1 << j)) == 0) {
                count++;
            }
        }
    }
    return count;
}

/*************************************************************************
 * 函数名称：PixelMask_Config(void)
 * 功能：像素点Mask坏点标记及配置
 **************************************************************************
 * 输入变量：     无
 * 输出变量：     无
 * 返回值：         无
 *-----------------------------------------------------------------------
 * 日期                              修改人                  修改内容
 *-----------------------------------------------------------------------
 * 2023.4.11      袁梓翔                  创建
 * xxxx.xx.xx     XXX         添加/修改了XXXXXXXXXXX
 *************************************************************************/
void PixelMask_Config(void)
{
    FIL fil1;
    FRESULT rc;
    
    u8 CamNum, AddrNum;
    u32 Pnt = 0, PntNum, bw;
    u8 bytes = 0, bits = 0, bitmask = 0xff;
    u8 MaskBuff[EMMC_DeadPixelMask_SIZE];
    
    char *mask_f_name[4] = {EMMC_DeadPixelMask_1_FILE_NAME, EMMC_DeadPixelMask_2_FILE_NAME,
    EMMC_DeadPixelMask_INIT_1_FILE_NAME, EMMC_DeadPixelMask_INIT_2_FILE_NAME};
    u32 mask_baseaddr[2] = {CAM1MASK_BASEADDR, CAM2MASK_BASEADDR};
    
    if(g_tEquipmentStatus.ucUpdateStatus == PIXEL_MASK_BEGIN) {
        /* 修改像素Mask坏点标记 */
        memset(MaskBuff, 0, EMMC_DeadPixelMask_SIZE);

        if(s_FirmwareUpdate.RecvPackageRelaxLen > 8) {
            CamNum = s_FirmwareUpdate.CompressPackRelax[7] - 1;
            AddrNum = CamNum % 2;
            Pnt = (s_FirmwareUpdate.RecvPackageRelaxLen - 8) / 4;
            
            rc = emmcReadData(mask_f_name[CamNum], MaskBuff, EMMC_DeadPixelMask_SIZE);
            
            for(PntNum = 0; PntNum < Pnt; PntNum++) {
                u32 value, x, y, bit_index;
                x = (u32)(s_FirmwareUpdate.CompressPackRelax[8 + 4 * PntNum] << 8) + s_FirmwareUpdate.CompressPackRelax[9 + 4 * PntNum];
                if(x != 0xffff && x >= EMMC_DeadPixelMask_COLUMN) {
                    return;
                }
                y = (u32)(s_FirmwareUpdate.CompressPackRelax[10 + 4 * PntNum] << 8) + s_FirmwareUpdate.CompressPackRelax[11 + 4 * PntNum];
                if(y != 0xffff && y >= EMMC_DeadPixelMask_ROW) {
                    return;
                }
                value = get_bit(s_FirmwareUpdate.CompressPackRelax, PntNum);
                if(x == 0xffff && y == 0xffff) {
                    if(value == 1) {
                        memset(MaskBuff, 0xff, EMMC_DeadPixelMask_SIZE);
                    }
                    else {
                        memset(MaskBuff, 0, EMMC_DeadPixelMask_SIZE);
                    }
                } else if(x == 0xffff) {
                    for(int column = 0; column < EMMC_DeadPixelMask_COLUMN; column++) {
                        bit_index = column + 9600 * y;
                        operate_bit(MaskBuff, bit_index, value);
                    }
                } else if(y == 0xffff) {
                    for(int row = 0; row < EMMC_DeadPixelMask_ROW; row++) {
                        bit_index = x + 9600 * row;
                        operate_bit(MaskBuff, bit_index, value);
                    }
                } else {
                    bit_index = x + 9600 * y;
                    operate_bit(MaskBuff, bit_index, value);
                }
            }
            memcpy((void *)mask_baseaddr[AddrNum], MaskBuff, EMMC_DeadPixelMask_SIZE); /* 赋值DDR上的坏点Mask */
            Xil_DCacheFlushRange(mask_baseaddr[AddrNum], EMMC_DeadPixelMask_SIZE);
            g_tEquipmentStatus.DeadPixelNum[AddrNum] = countBinaryZeros(mask_baseaddr[AddrNum], (EMMC_DeadPixelMask_SIZE - 4));
            memcpy(&MaskBuff[EMMC_DeadPixelMask_SIZE - 4], &g_tEquipmentStatus.DeadPixelNum[AddrNum], 4);
            rc = emmcWriteData(mask_f_name[CamNum], MaskBuff, EMMC_DeadPixelMask_SIZE, 0);
            g_tEquipmentStatus.MaskInfo[0] = Pnt;
            g_tEquipmentStatus.MaskInfo[1] = s_FirmwareUpdate.CompressPackRelax[7];
            bytes = Pnt / 8;
            bits = Pnt % 8;
            bitmask = bitmask << (8 - bits);
            for(int i = 0; i < bytes; i++) {
                g_tEquipmentStatus.MaskInfo[2 + i] = s_FirmwareUpdate.CompressPackRelax[i];
            }
            g_tEquipmentStatus.MaskInfo[2 + bytes] = s_FirmwareUpdate.CompressPackRelax[bytes] & bitmask;
        }
        s_FirmwareUpdate.e_State = INIT;
        s_FirmwareUpdate.RecvPackageType = 0xffff;
        g_tEquipmentStatus.ucUpdateStatus = INIT;
    }
    /* 像素坏点Mask恢复出厂配置 */
    if(g_sCamera1Setting.PixelMaskRstFlag) {
        g_sCamera1Setting.PixelMaskRstFlag = 0;
        CAM_PixelMask_ResFacSet(0);
    } else if(g_sCamera2Setting.PixelMaskRstFlag) {
        g_sCamera2Setting.PixelMaskRstFlag = 0;
        CAM_PixelMask_ResFacSet(1);
    } else {
        ;
    }
    
}

void prepParamSave(void)
{
    UINT bw;
	if(s_CamPrepFlag.cm1SaveFlag == 1)
	{
		s_CamPrepFlag.cm1SaveFlag = 0;
		f_open(&fil, EMMC_CAM1_PRPE_PARAM_FILE, FA_OPEN_EXISTING | FA_WRITE);
		f_lseek(&fil, 0);
		f_write(&fil, &s_Cam1PrepParam, sizeof(s_Cam1PrepParam), &bw);
		f_close(&fil);
	}
	if(s_CamPrepFlag.cm2SaveFlag == 1)
	{
		s_CamPrepFlag.cm2SaveFlag = 0;
		f_open(&fil, EMMC_CAM2_PRPE_PARAM_FILE, FA_OPEN_EXISTING | FA_WRITE);
		f_lseek(&fil, 0);
		f_write(&fil, &s_Cam2PrepParam, sizeof(s_Cam2PrepParam), &bw);
		f_close(&fil);
	}
}


void SetFpgaWorkMode(u8 ucNum, u32 uiValue)
{
	if(1 == ucNum){
		Xil_Out32(CAMERA_0_WORK_MODE, uiValue);
	    Xil_DCacheFlushRange(CAMERA_0_WORK_MODE, 4);
	}
	else{
		Xil_Out32(CAMERA_0_WORK_MODE, uiValue);
	    Xil_DCacheFlushRange(CAMERA_0_WORK_MODE, 4);
	}
}

void Set_Flat_Dark_process(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.Flat_Dark_process_en = uiValue;
        Xil_Out32(Cam0_Flat_Dark_process_en, uiValue);
 	    Xil_DCacheFlushRange(Cam0_Flat_Dark_process_en, 4);
     }
     else{
     	s_Cam2PrepParam.Flat_Dark_process_en = uiValue;
        Xil_Out32(Cam1_Flat_Dark_process_en, uiValue);
  	    Xil_DCacheFlushRange(Cam1_Flat_Dark_process_en, 4);
     }
}
u32 Read_Flat_Dark_process(u8 ucNum)
{
    if(1 == ucNum){
	    Xil_DCacheFlushRange(Cam0_Flat_Dark_process_en, 4);
    	return Xil_In32(Cam0_Flat_Dark_process_en);
     }
     else{
 	    Xil_DCacheFlushRange(Cam1_Flat_Dark_process_en, 4);
    	return Xil_In32(Cam1_Flat_Dark_process_en);
     }
}

void Set_block_size_0(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.block_size_0 = uiValue;
         Xil_Out32(Cam0_block_size_0, uiValue);
  	    Xil_DCacheFlushRange(Cam0_block_size_0, 4);
     }
     else{
     	s_Cam2PrepParam.block_size_0 = uiValue;
         Xil_Out32(Cam1_block_size_0, uiValue);
  	    Xil_DCacheFlushRange(Cam1_block_size_0, 4);
     }
}
u32 Read_block_size_0(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_block_size_0, 4);
    	return Xil_In32(Cam0_block_size_0);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_block_size_0, 4);
    	 return Xil_In32(Cam1_block_size_0);
     }
}

void Set_block_size_1(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.block_size_1 = uiValue;
         Xil_Out32(Cam0_block_size_1, uiValue);
  	    Xil_DCacheFlushRange(Cam0_block_size_1, 4);
     }
     else{
     	s_Cam2PrepParam.block_size_1 = uiValue;
         Xil_Out32(Cam1_block_size_1, uiValue);
  	    Xil_DCacheFlushRange(Cam1_block_size_1, 4);
     }
}
u32 Read_block_size_1(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_block_size_1, 4);
    	return Xil_In32(Cam0_block_size_1);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_block_size_1, 4);
    	 return Xil_In32(Cam1_block_size_1);
     }
}

void Set_blcok0_Nmax0(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.blcok0_Nmax0 = uiValue;
         Xil_Out32(Cam0_blcok0_Nmax0, uiValue);
  	    Xil_DCacheFlushRange(Cam0_blcok0_Nmax0, 4);
     }
     else{
     	s_Cam2PrepParam.blcok0_Nmax0 = uiValue;
         Xil_Out32(Cam1_blcok0_Nmax0, uiValue);
  	    Xil_DCacheFlushRange(Cam1_blcok0_Nmax0, 4);
     }
}
u32 Read_blcok0_Nmax0(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_blcok0_Nmax0, 4);
    	return Xil_In32(Cam0_blcok0_Nmax0);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_blcok0_Nmax0, 4);
    	 return  Xil_In32(Cam1_blcok0_Nmax0);
     }
}

void Set_blcok1_Nmax1(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.blcok1_Nmax1 = uiValue;
         Xil_Out32(Cam0_blcok1_Nmax1, uiValue);
  	    Xil_DCacheFlushRange(Cam0_blcok1_Nmax1, 4);
     }
     else{
     	s_Cam2PrepParam.blcok1_Nmax1 = uiValue;
         Xil_Out32(Cam1_blcok1_Nmax1, uiValue);
  	    Xil_DCacheFlushRange(Cam1_blcok1_Nmax1, 4);
     }
}
u32 Read_blcok1_Nmax1(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_blcok1_Nmax1, 4);
    	return Xil_In32(Cam0_blcok1_Nmax1);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_blcok1_Nmax1, 4);
    	 return Xil_In32(Cam1_blcok1_Nmax1);
     }
}

void Set_blcok_Nmin(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.blcok_Nmin = uiValue;
         Xil_Out32(Cam0_blcok_Nmin, uiValue);
  	    Xil_DCacheFlushRange(Cam0_blcok_Nmin, 4);
     }
     else{
     	s_Cam2PrepParam.blcok_Nmin = uiValue;
         Xil_Out32(Cam1_blcok_Nmin, uiValue);
  	    Xil_DCacheFlushRange(Cam1_blcok_Nmin, 4);
     }
}
u32 Read_blcok_Nmin(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_blcok_Nmin, 4);
    	return Xil_In32(Cam0_blcok_Nmin);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_blcok_Nmin, 4);
    	 return  Xil_In32(Cam1_blcok_Nmin);
     }
}

void Set_corner_cnt(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.corner_cnt = uiValue;
         Xil_Out32(Cam0_corner_cnt, uiValue);
  	    Xil_DCacheFlushRange(Cam0_corner_cnt, 4);
     }
     else{
     	s_Cam2PrepParam.corner_cnt = uiValue;
         Xil_Out32(Cam1_corner_cnt, uiValue);
  	    Xil_DCacheFlushRange(Cam1_corner_cnt, 4);
     }
}
u32 Read_corner_cnt(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_corner_cnt, 4);
    	return Xil_In32(Cam0_corner_cnt);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_corner_cnt, 4);
    	 return Xil_In32(Cam1_corner_cnt);
     }
}

void Set_atomosphere_thresh(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.atomosphere_thresh = uiValue;
         Xil_Out32(Cam0_atomosphere_thresh, uiValue);
  	    Xil_DCacheFlushRange(Cam0_atomosphere_thresh, 4);
     }
     else{
     	s_Cam2PrepParam.atomosphere_thresh = uiValue;
         Xil_Out32(Cam1_atomosphere_thresh, uiValue);
  	    Xil_DCacheFlushRange(Cam1_atomosphere_thresh, 4);
     }
}
u32 Read_atomosphere_thresh(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_atomosphere_thresh, 4);
    	return Xil_In32(Cam0_atomosphere_thresh);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_atomosphere_thresh, 4);
    	 return  Xil_In32(Cam1_atomosphere_thresh);
     }
}

void Set_max_cfs_npix(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.max_cfs_npix = uiValue;
         Xil_Out32(Cam0_max_cfs_npix, uiValue);
  	    Xil_DCacheFlushRange(Cam0_max_cfs_npix, 4);
     }
     else{
     	s_Cam2PrepParam.max_cfs_npix = uiValue;
         Xil_Out32(Cam1_max_cfs_npix, uiValue);
  	    Xil_DCacheFlushRange(Cam1_max_cfs_npix, 4);
     }
}
u32 Read_max_cfs_npix(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_max_cfs_npix, 4);
    	return Xil_In32(Cam0_max_cfs_npix);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_max_cfs_npix, 4);
    	 return  Xil_In32(Cam1_max_cfs_npix);
     }
}

void Set_binarized_thresh(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.binarized_thresh = uiValue;
         Xil_Out32(Cam0_binarized_thresh, uiValue);
  	    Xil_DCacheFlushRange(Cam0_binarized_thresh, 4);
     }
     else{
     	s_Cam2PrepParam.binarized_thresh = uiValue;
         Xil_Out32(Cam1_binarized_thresh, uiValue);
  	    Xil_DCacheFlushRange(Cam1_binarized_thresh, 4);
     }
}
u32 Read_binarized_thresh(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_binarized_thresh, 4);
    	return Xil_In32(Cam0_binarized_thresh);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_binarized_thresh, 4);
    	 return  Xil_In32(Cam1_binarized_thresh);
     }
}

void Set_saturated_thresh(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.saturated_thresh = uiValue;
         Xil_Out32(Cam0_saturated_thresh, uiValue);
  	    Xil_DCacheFlushRange(Cam0_saturated_thresh, 4);
     }
     else{
     	s_Cam2PrepParam.saturated_thresh = uiValue;
         Xil_Out32(Cam1_saturated_thresh, uiValue);
  	    Xil_DCacheFlushRange(Cam1_saturated_thresh, 4);
     }
}
u32 Read_saturated_thresh(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_saturated_thresh, 4);
    	return Xil_In32(Cam0_saturated_thresh);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_saturated_thresh, 4);
    	 return  Xil_In32(Cam1_saturated_thresh);
     }
}


void Set_object_thresh(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.object_thresh = uiValue;
         Xil_Out32(Cam0_object_thresh, uiValue);
  	    Xil_DCacheFlushRange(Cam0_object_thresh, 4);
     }
     else{
     	s_Cam2PrepParam.object_thresh = uiValue;
         Xil_Out32(Cam1_object_thresh, uiValue);
  	    Xil_DCacheFlushRange(Cam1_object_thresh, 4);
     }
}
u32 Read_object_thresh(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_object_thresh, 4);
    	return Xil_In32(Cam0_object_thresh);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_object_thresh, 4);
    	 return  Xil_In32(Cam1_object_thresh);
     }
}

void Set_min_streak_npix(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.min_streak_npix = uiValue;
         Xil_Out32(Cam0_min_streak_npix, uiValue);
  	    Xil_DCacheFlushRange(Cam0_min_streak_npix, 4);
     }
     else{
     	s_Cam2PrepParam.min_streak_npix = uiValue;
         Xil_Out32(Cam1_min_streak_npix, uiValue);
  	    Xil_DCacheFlushRange(Cam1_min_streak_npix, 4);
     }
}
u32 Read_min_streak_npix(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_min_streak_npix, 4);
    	return Xil_In32(Cam0_min_streak_npix);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_min_streak_npix, 4);
    	 return  Xil_In32(Cam1_min_streak_npix);
     }
}

void Set_max_streak_npix(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.max_streak_npix = uiValue;
         Xil_Out32(Cam0_max_streak_npix, uiValue);
  	    Xil_DCacheFlushRange(Cam0_max_streak_npix, 4);
     }
     else{
     	s_Cam2PrepParam.max_streak_npix = uiValue;
         Xil_Out32(Cam1_max_streak_npix, uiValue);
  	    Xil_DCacheFlushRange(Cam1_max_streak_npix, 4);
     }
}
u32 Read_max_streak_npix(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_max_streak_npix, 4);
    	return Xil_In32(Cam0_max_streak_npix);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_max_streak_npix, 4);
    	 return  Xil_In32(Cam1_max_streak_npix);
     }
}

void Set_porosity_grade(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.porosity_grade = uiValue;
         Xil_Out32(Cam0_porosity_grade, uiValue);
  	    Xil_DCacheFlushRange(Cam0_porosity_grade, 4);
     }
     else{
     	s_Cam2PrepParam.porosity_grade = uiValue;
         Xil_Out32(Cam1_porosity_grade, uiValue);
  	    Xil_DCacheFlushRange(Cam1_porosity_grade, 4);
     }
}
u32 Read_porosity_grade(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_porosity_grade, 4);
    	return Xil_In32(Cam0_porosity_grade);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_porosity_grade, 4);
    	 return  Xil_In32(Cam1_porosity_grade);
     }
}

void Set_Target_detection_num(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.Target_detection_num = uiValue;
         Xil_Out32(Cam0_Target_detection_num, uiValue);
  	    Xil_DCacheFlushRange(Cam0_Target_detection_num, 4);
     }
     else{
     	s_Cam2PrepParam.Target_detection_num = uiValue;
         Xil_Out32(Cam1_Target_detection_num, uiValue);
  	    Xil_DCacheFlushRange(Cam1_Target_detection_num, 4);
     }
}
u32 Read_Target_detection_num(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_Target_detection_num, 4);
    	return Xil_In32(Cam0_Target_detection_num);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_Target_detection_num, 4);
    	 return  Xil_In32(Cam1_Target_detection_num);
     }
}


void Set_sub_filtering_en(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.sub_filtering_en = uiValue;
         Xil_Out32(Cam0_sub_filtering_en, uiValue);
  	    Xil_DCacheFlushRange(Cam0_sub_filtering_en, 4);
     }
     else{
     	s_Cam2PrepParam.sub_filtering_en = uiValue;
         Xil_Out32(Cam1_sub_filtering_en, uiValue);
  	    Xil_DCacheFlushRange(Cam1_sub_filtering_en, 4);
     }
}
u32 Read_sub_filtering_en(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_sub_filtering_en, 4);
    	return Xil_In32(Cam0_sub_filtering_en);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_sub_filtering_en, 4);
    	 return  Xil_In32(Cam1_sub_filtering_en);
     }
}

void Set_atomosphere_mask_en(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.atomosphere_mask_en = uiValue;
         Xil_Out32(Cam0_atomosphere_mask_en, uiValue);
  	    Xil_DCacheFlushRange(Cam0_atomosphere_mask_en, 4);
     }
     else{
     	s_Cam2PrepParam.atomosphere_mask_en = uiValue;
         Xil_Out32(Cam1_atomosphere_mask_en, uiValue);
  	    Xil_DCacheFlushRange(Cam1_atomosphere_mask_en, 4);
     }
}
u32 Read_atomosphere_mask_en(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_atomosphere_mask_en, 4);
    	return Xil_In32(Cam0_atomosphere_mask_en);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_atomosphere_mask_en, 4);
    	 return  Xil_In32(Cam1_atomosphere_mask_en);
     }
}


void Set_fill_defect_mask_en(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.fill_defect_mask_en = uiValue;
         Xil_Out32(Cam0_fill_defect_mask_en, uiValue);
  	    Xil_DCacheFlushRange(Cam0_fill_defect_mask_en, 4);
     }
     else{
     	s_Cam2PrepParam.fill_defect_mask_en = uiValue;
         Xil_Out32(Cam1_fill_defect_mask_en, uiValue);
  	    Xil_DCacheFlushRange(Cam1_fill_defect_mask_en, 4);
     }
}
u32 Read_fill_defect_mask_en(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_fill_defect_mask_en, 4);
    	return Xil_In32(Cam0_fill_defect_mask_en);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_fill_defect_mask_en, 4);
    	 return  Xil_In32(Cam1_fill_defect_mask_en);
     }
}

void Set_bright_region_thresh(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.bright_region_thresh = uiValue;
         Xil_Out32(Cam0_bright_region_thresh, uiValue);
  	    Xil_DCacheFlushRange(Cam0_bright_region_thresh, 4);
     }
     else{
     	s_Cam2PrepParam.bright_region_thresh = uiValue;
         Xil_Out32(Cam1_bright_region_thresh, uiValue);
  	    Xil_DCacheFlushRange(Cam1_bright_region_thresh, 4);
     }
}
u32 Read_bright_region_thresh(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_bright_region_thresh, 4);
    	return Xil_In32(Cam0_bright_region_thresh);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_bright_region_thresh, 4);
    	 return  Xil_In32(Cam1_bright_region_thresh);
     }
}

void Set_flat_dark_dn_offset(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.flat_dark_dn_offset = uiValue;
         Xil_Out32(Cam0_flat_dark_dn_offset, uiValue);
  	    Xil_DCacheFlushRange(Cam0_flat_dark_dn_offset, 4);
     }
     else{
     	s_Cam2PrepParam.flat_dark_dn_offset = uiValue;
         Xil_Out32(Cam1_flat_dark_dn_offset, uiValue);
  	    Xil_DCacheFlushRange(Cam1_flat_dark_dn_offset, 4);
     }
}
u32 Read_flat_dark_dn_offset(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_flat_dark_dn_offset, 4);
    	return Xil_In32(Cam0_flat_dark_dn_offset);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_flat_dark_dn_offset, 4);
    	 return  Xil_In32(Cam1_flat_dark_dn_offset);
     }
}

void Set_3sita_value(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.sita3_value = uiValue;
         Xil_Out32(Cam0_3sita_value, uiValue);
  	    Xil_DCacheFlushRange(Cam0_3sita_value, 4);
     }
     else{
     	s_Cam2PrepParam.sita3_value = uiValue;
         Xil_Out32(Cam1_3sita_value, uiValue);
  	    Xil_DCacheFlushRange(Cam1_3sita_value, 4);
     }
}
u32 Read_3sita_value(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_3sita_value, 4);
    	return Xil_In32(Cam0_3sita_value);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_3sita_value, 4);
    	 return  Xil_In32(Cam1_3sita_value);
     }
}

void Set_cali_norm_value(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.cali_norm_value = uiValue;
         Xil_Out32(Cam0_cali_norm_value, uiValue);
  	    Xil_DCacheFlushRange(Cam0_cali_norm_value, 4);
     }
     else{
     	s_Cam2PrepParam.cali_norm_value = uiValue;
         Xil_Out32(Cam1_cali_norm_value, uiValue);
  	    Xil_DCacheFlushRange(Cam1_cali_norm_value, 4);
     }
}
u32 Read_cali_norm_value(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_cali_norm_value, 4);
    	return Xil_In32(Cam0_cali_norm_value);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_cali_norm_value, 4);
    	 return  Xil_In32(Cam1_cali_norm_value);
     }
}

void Set_scale_factor(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.scale_factor = uiValue;
         Xil_Out32(Cam0_scale_factor, uiValue);
  	    Xil_DCacheFlushRange(Cam0_scale_factor, 4);
     }
     else{
     	s_Cam2PrepParam.scale_factor = uiValue;
         Xil_Out32(Cam1_scale_factor, uiValue);
  	    Xil_DCacheFlushRange(Cam1_scale_factor, 4);
     }
}
u32 Read_scale_factor(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_scale_factor, 4);
    	return Xil_In32(Cam0_scale_factor);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_scale_factor, 4);
    	 return  Xil_In32(Cam1_scale_factor);
     }
}
void Set_length_width_ratio(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.length_width_ratio = uiValue;
         Xil_Out32(Cam0_length_width_ratio, uiValue);
  	    Xil_DCacheFlushRange(Cam0_length_width_ratio, 4);
     }
     else{
     	s_Cam2PrepParam.length_width_ratio = uiValue;
         Xil_Out32(Cam1_length_width_ratio, uiValue);
  	    Xil_DCacheFlushRange(Cam1_length_width_ratio, 4);
     }
}
u32 Read_length_width_ratio(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_length_width_ratio, 4);
    	return Xil_In32(Cam0_length_width_ratio);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_length_width_ratio, 4);
    	 return  Xil_In32(Cam1_length_width_ratio);
     }
}
void Set_corr_coef_thresh0(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.corr_coef_thresh0 = uiValue;
         Xil_Out32(Cam0_corr_coef_thresh0, uiValue);
  	    Xil_DCacheFlushRange(Cam0_corr_coef_thresh0, 4);
     }
     else{
     	s_Cam2PrepParam.corr_coef_thresh0 = uiValue;
         Xil_Out32(Cam1_corr_coef_thresh0, uiValue);
  	    Xil_DCacheFlushRange(Cam1_corr_coef_thresh0, 4);
     }
}
u32 Read_corr_coef_thresh0(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_corr_coef_thresh0, 4);
    	return Xil_In32(Cam0_corr_coef_thresh0);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_corr_coef_thresh0, 4);
    	 return  Xil_In32(Cam1_corr_coef_thresh0);
     }
}
void Set_corr_coef_thresh1(u8 ucNum, u32 uiValue)
{
    if(1 == ucNum){
    	s_Cam1PrepParam.corr_coef_thresh1 = uiValue;
         Xil_Out32(Cam0_corr_coef_thresh1, uiValue);
  	    Xil_DCacheFlushRange(Cam0_corr_coef_thresh1, 4);
     }
     else{
     	s_Cam2PrepParam.corr_coef_thresh1 = uiValue;
         Xil_Out32(Cam1_corr_coef_thresh1, uiValue);
  	    Xil_DCacheFlushRange(Cam1_corr_coef_thresh1, 4);
     }
}
u32 Read_corr_coef_thresh1(u8 ucNum)
{
    if(1 == ucNum){
 	    Xil_DCacheFlushRange(Cam0_corr_coef_thresh1, 4);
    	return Xil_In32(Cam0_corr_coef_thresh1);
     }
     else{
  	    Xil_DCacheFlushRange(Cam1_corr_coef_thresh1, 4);
    	 return  Xil_In32(Cam1_corr_coef_thresh1);
     }
}


/*************************************************************************
* 函数名称：	SetFpgaExposureTime
* 功能：		配置fpga获取相机图像的的曝光时间
**************************************************************************
* 全局变量：		无
* 输入变量：		无
* 输出变量：		无
* 返回值：                       	无
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.10.28            张海峰         		 创建
* XXXX.XX.XX    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
void SetFpgaExposureTime(u8 ucNum, u32 uiValue)
{
	if(1 == ucNum){
		Xil_Out32(CAMERA_0_EXPOSURE, uiValue);
		g_tEquipmentStatus.uiCamera_1_ExposureTime = uiValue;
	}
	else{
		Xil_Out32(CAMERA_1_EXPOSURE, uiValue);
		g_tEquipmentStatus.uiCamera_2_ExposureTime = uiValue;
	}
}

/*************************************************************************
* 函数名称：	SetFpgaPicNum
* 功能：		配置fpga需要接收的图片的数量
**************************************************************************
* 全局变量：		无
* 输入变量：		无
* 输出变量：		无
* 返回值：                       	无
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.10.28            张海峰         		 创建
* XXXX.XX.XX    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
void SetFpgaPicNum(u8 ucNum, u32 uiValue)
{
	if(1 == ucNum){
		Xil_Out32(CAMERA_0_PIC_NUM, uiValue);
 	    Xil_DCacheFlushRange(CAMERA_0_PIC_NUM, 4);
	}
	else{
		Xil_Out32(CAMERA_1_PIC_NUM, uiValue);
 	    Xil_DCacheFlushRange(CAMERA_1_PIC_NUM, 4);
	}
}

/*************************************************************************
* 函数名称：	SetFpga3CEachPic
* 功能：		配置fpga接收图像3c信息的数量
**************************************************************************
* 全局变量：		无
* 输入变量：		无
* 输出变量：		无
* 返回值：                       	无
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.10.28            张海峰         		 创建
* XXXX.XX.XX    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
void SetFpga3CEachPic(u8 ucNum, u32 uiValue)
{
	if(1 == ucNum){
		Xil_Out32(CAMERA_0_EACHPIC_3C_NUM, uiValue);
 	    Xil_DCacheFlushRange(CAMERA_0_EACHPIC_3C_NUM, 4);
	}
	else{
		Xil_Out32(CAMERA_1_EACHPIC_3C_NUM, uiValue);
 	    Xil_DCacheFlushRange(CAMERA_1_EACHPIC_3C_NUM, 4);
	}
}

/*************************************************************************
* 函数名称：	SetFpgaGain
* 功能：		配置fpga接收图像的增益
**************************************************************************
* 全局变量：		无
* 输入变量：		无
* 输出变量：		无
* 返回值：                       	无
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.10.28            张海峰         		 创建
* XXXX.XX.XX    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
void SetFpgaGain(u8 ucNum, u32 uiValue)
{
	if(1 == ucNum){
		Xil_Out32(CAMERA_0_GAIN, uiValue);
	}
	else{
		Xil_Out32(CAMERA_1_GAIN, uiValue);
	}
}

/*************************************************************************
* 函数名称：	SetFpgaShotTimeLapse
* 功能：		配置fpga接收图像的连拍间隔时间
**************************************************************************
* 全局变量：		无
* 输入变量：		无
* 输出变量：		无
* 返回值：                       	无
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.12.28            张海峰         		 创建
* XXXX.XX.XX    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
void SetFpgaShotTimeLapse(u8 ucNum, u32 uiValue)
{
	if(1 == ucNum){
		Xil_Out32(CAMERA_0_SHOT_TIME_LAPSE, uiValue);
	}
	else{
		Xil_Out32(CAMERA_1_SHOT_TIME_LAPSE, uiValue);
	}
}

/*************************************************************************
* 函数名称：	SetFpgaShot
* 功能：		配置fpga接收图像的启动
**************************************************************************
* 全局变量：		无
* 输入变量：		无
* 输出变量：		无
* 返回值：                       	无
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.12.28            张海峰         		 创建
* XXXX.XX.XX    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
void SetFpgaShot(u8 ucNum)
{
	if(1 == ucNum){
		Xil_Out32(CAMERA_0_START_SHOT, 1);
		Xil_Out32(CAMERA_0_START_SHOT, 0);
	}
	else{
		Xil_Out32(CAMERA_1_START_SHOT, 1);
		Xil_Out32(CAMERA_1_START_SHOT, 0);
	}
}

/*************************************************************************
* 函数名称：	SetFpgaTime
* 功能：		配置fpga时间寄存器
**************************************************************************
* 全局变量：		无
* 输入变量：		无
* 输出变量：		无
* 返回值：                       	无
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.12.28            张海峰         		 创建
* XXXX.XX.XX    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
void SetFpgaTime(u32 uiSecond)
{
	Xil_Out32(TIME_INPUT, uiSecond);
}

u32 ReadPlLockedState(void)
{
	return Xil_In32(FPGA_TIME_LOCKED);
}

u32 ReadPlRecvImageNum(u8 ucNum)
{
   if(ucNum == 1){
       Xil_DCacheFlushRange(CAMERA_0_PL_RECV_NUM, 4);
      return Xil_In32(CAMERA_0_PL_RECV_NUM);
   }
   else{
       Xil_DCacheFlushRange(CAMERA_1_PL_RECV_NUM, 4);
       return Xil_In32(CAMERA_1_PL_RECV_NUM);
   }
}
u32 ReadPlSendImageNum(u8 ucNum)
{
   if(ucNum == 1){
       Xil_DCacheFlushRange(CAMERA_0_LVDS_SEND_NUM, 4);
      return Xil_In32(CAMERA_0_LVDS_SEND_NUM);
   }
   else{
       Xil_DCacheFlushRange(CAMERA_1_LVDS_SEND_NUM, 4);
       return Xil_In32(CAMERA_1_LVDS_SEND_NUM);
   }
}
u32 ReadPlObjNum(u8 ucNum)
{
   if(ucNum == 1){
       Xil_DCacheFlushRange(CAMERA_0_PL_OBJ_NUM, 4);
      return Xil_In32(CAMERA_0_PL_OBJ_NUM);
   }
   else{
       Xil_DCacheFlushRange(CAMERA_1_PL_OBJ_NUM, 4);
       return Xil_In32(CAMERA_1_PL_OBJ_NUM);
   }
}

u32 ReadPlProcessNum(u8 ucNum)
{
   if(ucNum == 1){
       Xil_DCacheFlushRange(CAMERA_0_PL_FINISH_NUM, 4);
      return Xil_In32(CAMERA_0_PL_FINISH_NUM);
   }
   else{
       Xil_DCacheFlushRange(CAMERA_1_PL_FINISH_NUM, 4);
       return Xil_In32(CAMERA_1_PL_FINISH_NUM);
   }
}

u32 ReadPlDDRPicNum(u8 ucNum)
{
   if(ucNum == 1){
       Xil_DCacheFlushRange(CAMERA_0_DDR_PIC_NUM, 4);
      return Xil_In32(CAMERA_0_DDR_PIC_NUM);
   }
   else{
       Xil_DCacheFlushRange(CAMERA_1_DDR_PIC_NUM, 4);
       return Xil_In32(CAMERA_1_DDR_PIC_NUM);
   }
}

u32 ReadlvdsPackNum(void)
{
    Xil_DCacheFlushRange(CAMERA_LVDS_PACK_NUM, 4);
    return Xil_In32(CAMERA_LVDS_PACK_NUM);
}

u32 ReadMsTime(void)
{
    Xil_DCacheFlushRange(TIME_MSECOND_OUTPUT, 4);
    return Xil_In32(TIME_MSECOND_OUTPUT);
}

u32 ReadSTime(void)
{
    Xil_DCacheFlushRange(TIME_SECOND_OUTPUT, 4);
    return Xil_In32(TIME_SECOND_OUTPUT);
}
u32 Read_pl_run_status(u8 ucNum)
{
	if(ucNum == 1){
	   Xil_DCacheFlushRange(Cam0_pl_run_status, 4);
	  return Xil_In32(Cam0_pl_run_status);
	}
	else{
	   Xil_DCacheFlushRange(Cam1_pl_run_status, 4);
	   return Xil_In32(Cam1_pl_run_status);
	}
}

void SetCMA_Prep_Para(u8 Command, u8 *puacData, u8 Num)
{
    u32 uiValue = 0;
    switch(Command)
    {
        case 0:
        {
            uiValue = puacData[5];
            Set_Flat_Dark_process(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 1:
        {
            uiValue = puacData[5]<<8;
            uiValue |= puacData[4];
            Set_block_size_0(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 2:
        {
            uiValue = puacData[5]<<8;
            uiValue |= puacData[4];
            Set_block_size_1(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 3:
        {
            uiValue = puacData[5]<<8;
            uiValue |= puacData[4];
            Set_blcok0_Nmax0(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 4:
        {
            uiValue = puacData[5]<<8;
            uiValue |= puacData[4];
            Set_blcok1_Nmax1(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 5:
        {
            uiValue = puacData[5]<<8;
            uiValue |= puacData[4];
            Set_blcok_Nmin(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 6:
        {
            uiValue = puacData[5]<<8;
            uiValue |= puacData[4];
            Set_corner_cnt(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 7:
        {
            uiValue = puacData[5]<<8;
            uiValue |= puacData[4];
            Set_atomosphere_thresh(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 8:
        {
            uiValue = puacData[5]<<8;
            uiValue |= puacData[4];
            Set_max_cfs_npix(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 9:
        {
            uiValue = puacData[5]<<8;
            uiValue |= puacData[4];
            Set_binarized_thresh(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 0x0A:
        {
            uiValue = puacData[5]<<24;
            uiValue |= puacData[4]<<16;
            uiValue |= puacData[3]<<8;
            uiValue |= puacData[2];
            Set_saturated_thresh(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 0x0B:
        {
            uiValue = puacData[5]<<8;
            uiValue |= puacData[4];
            Set_object_thresh(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 0x0C:
        {
            uiValue = puacData[5]<<8;
            uiValue |= puacData[4];
            Set_min_streak_npix(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 0x0D:
        {
            uiValue = puacData[5]<<8;
            uiValue |= puacData[4];
            Set_max_streak_npix(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 0x0E:
        {
            uiValue = puacData[5];
            Set_porosity_grade(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 0x0F:
        {
            uiValue = puacData[5];
            Set_Target_detection_num(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 0x10:
        {
            uiValue = puacData[5];
            Set_sub_filtering_en(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 0x11:
        {
            uiValue = puacData[5];
            Set_atomosphere_mask_en(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 0x12:
        {
            uiValue = puacData[5];
            Set_fill_defect_mask_en(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 0x13:
        {
            uiValue = puacData[5]<<8;
            uiValue |= puacData[4];
            Set_bright_region_thresh(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 0x14:
        {
            uiValue = puacData[5]<<8;
            uiValue |= puacData[4];
            Set_3sita_value(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 0x15:
        {
        	uiValue = puacData[5];
        	caliChooseData(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 0x16:
        {
            uiValue = puacData[5]<<8;
            uiValue |= puacData[4];
            Set_flat_dark_dn_offset(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 0x17:
        {
            uiValue = puacData[5]<<8;
            uiValue |= puacData[4];
            Set_cali_norm_value(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 0x18:
        {
            uiValue = puacData[5];
            Set_scale_factor(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 0x19:
        {
            uiValue = puacData[5]<<8;
            uiValue |= puacData[4];
            Set_length_width_ratio(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 0x1A:
        {
            uiValue = puacData[5]<<8;
            uiValue |= puacData[4];
            Set_corr_coef_thresh0(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 0x1B:
        {
            uiValue = puacData[5]<<8;
            uiValue |= puacData[4];
            Set_corr_coef_thresh1(Num,uiValue);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }

    }
}

/*port:相机选择；num:当前第几个辅助信息；AdditDataPtr：辅助信息头指针*/
void WriteAdditData(u8 port, u16 num, S_tAdditData* AdditDataPtr)
{
	if(num <= 0){
		num = 1;
	}
	num--;

	if(port == 1){
		for(u8 index = 0;index < GNCDATALEN;index++)
		{
			Xil_Out8((CAM1GNC_BASEADDR+num*ADDITDATALEN+index), AdditDataPtr->GncRelax[index]);
		}
		Xil_Out32((CAM1GNC_BASEADDR+num*ADDITDATALEN+GNCDATALEN),AdditDataPtr->ShootMomentS);
		Xil_Out16((CAM1GNC_BASEADDR+num*ADDITDATALEN+GNCDATALEN+4),AdditDataPtr->ShootMomentMs);
		Xil_Out32((CAM1GNC_BASEADDR+num*ADDITDATALEN+GNCDATALEN+6),AdditDataPtr->ExpTimeMs);
		Xil_Out8((CAM1GNC_BASEADDR+num*ADDITDATALEN+GNCDATALEN+10),AdditDataPtr->Gain);
		Xil_Out8((CAM1GNC_BASEADDR+num*ADDITDATALEN+GNCDATALEN+11),AdditDataPtr->Mode);
		g_tEquipmentStatus.pl_1_status = Read_pl_run_status(1);
		Xil_Out16((CAM1GNC_BASEADDR+num*ADDITDATALEN+GNCDATALEN+12),g_tEquipmentStatus.pl_1_status);
		Xil_DCacheFlushRange(CAM1GNC_BASEADDR+num*ADDITDATALEN, ADDITDATALEN);
	}
	else{
		for(u8 index = 0;index < GNCDATALEN;index++)
		{
			Xil_Out8((CAM2GNC_BASEADDR+num*ADDITDATALEN+index), AdditDataPtr->GncRelax[index]);
		}
		Xil_Out32((CAM2GNC_BASEADDR+num*ADDITDATALEN+GNCDATALEN),AdditDataPtr->ShootMomentS);
		Xil_Out16((CAM2GNC_BASEADDR+num*ADDITDATALEN+GNCDATALEN+4),AdditDataPtr->ShootMomentMs);
		Xil_Out32((CAM2GNC_BASEADDR+num*ADDITDATALEN+GNCDATALEN+6),AdditDataPtr->ExpTimeMs);
		Xil_Out8((CAM2GNC_BASEADDR+num*ADDITDATALEN+GNCDATALEN+10),AdditDataPtr->Gain);
		Xil_Out8((CAM2GNC_BASEADDR+num*ADDITDATALEN+GNCDATALEN+11),AdditDataPtr->Mode);
		g_tEquipmentStatus.pl_2_status = Read_pl_run_status(2);
		Xil_Out16((CAM1GNC_BASEADDR+num*ADDITDATALEN+GNCDATALEN+12),g_tEquipmentStatus.pl_2_status);
		Xil_DCacheFlushRange(CAM2GNC_BASEADDR+num*ADDITDATALEN, ADDITDATALEN);
	}
}

u8 ReadCaliState(u8 port, u8 type)
{
	if(port == CAMERA_ONE && type == DARK){
		return Xil_In32(CAM1DARK_STATE);
	}
	else if(port == CAMERA_ONE && type == FLAT){
		return Xil_In32(CAM1FLAT_STATE);
	}
	if(port == CAMERA_TWO && type == DARK){
		return Xil_In32(CAM2DARK_STATE);
	}
	if(port == CAMERA_TWO && type == FLAT){
		return Xil_In32(CAM2FLAT_STATE);
	}
	return 0;
}
